package com.share.test.example.groovy;

import com.share.common.utils.DbUtil;
import com.share.common.utils.FileUtil;
import com.share.common.utils.HttpUtil;
import com.share.example.selenium.config.DriverConfigure;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;

import java.io.File;
import java.sql.Connection;
import java.util.Properties;

@Slf4j
public class GroovyMain {
    private final static String GROOVY_FILENAME = "Groovy.groovy";
    private final static String GROOVY_METHOD = "parse";

    private static GroovyObject groovyInstacnce;
    private static String fileHome;

    static {
        try {
            //获取代码跟路径
            String classPath = new File(GroovyMain.class.getResource("./").getPath()).getAbsolutePath();
            classPath = classPath.replace("target\\classes", "src\\main\\java");
            fileHome = classPath.replace("target\\test-classes", "src\\test\\java");

            //初始化groovy对象
            Class aClass = new GroovyClassLoader().parseClass(new File(fileHome + File.separator + GROOVY_FILENAME));
            groovyInstacnce = (GroovyObject) aClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 仅仅普通java环境,非spring环境
     */
    public static String mainTest() {
        WebDriver driver = DriverConfigure.webDriver();
        Object value = groovyInstacnce.invokeMethod(GROOVY_METHOD, driver);
        return value == null ? null : value.toString();
    }

    /**
     * http请求测试
     */
    public static String httpTest(Long id) {
        try {
            //1.插入数据库
            if (id == null) {
                id = insertDb();
                System.out.println("id=" + id);
            }

            //2.发起执行
            String url = "http://127.0.0.1:8521/selenium/common/exeGroovyById?id=" + id;
            return HttpUtil.get(url, -1);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Long insertDb() throws Exception {
        //1.把数据插入到数据库
        String read = FileUtil.read(fileHome + File.separator + GROOVY_FILENAME);

        //2.请求执行
        Properties properties = FileUtil.readPropertiesFromCp("application-dev.properties");
        String driver = properties.getProperty("spring.datasource.driver-class-name");
        String url = properties.getProperty("spring.datasource.url");
        String user = properties.getProperty("spring.datasource.username");
        String passwd = properties.getProperty("spring.datasource.password");
        String OPTION = "?serverTimezone=UTC";
        Connection con = DbUtil.getConnection(driver, url + OPTION, user, passwd);
        String sql = String.format("INSERT INTO base_conf(`code`, `value`, `data_type`,  `status`) VALUES ( 'amazon_buy', '%s', 'groovy', '1')", read);
        Long id = DbUtil.insertOneRecord(con, sql);
        return id;
    }


    public static void main(String[] args) {
        String s = httpTest(null);
//        String s = mainTest(null);
        System.out.println(s);
    }
}
